package com.wisenet.controller.client;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.wisenet.annotation.LoginValidate;
import com.wisenet.controller.PlatformController;
import com.wisenet.util.TencentOssUtil;

/**
 * 文件上传控制类
 * @author fzh
 */
@RestController
@RequestMapping("/file")
public class UploadController extends PlatformController {
	// 日志
	public static Logger logger = Logger.getLogger(UploadController.class);
	
	/**
	 * 上传图片到腾讯云
	 * @param myimage
	 * @param folder
	 * @return
	 */
	@LoginValidate(value = false)
	@RequestMapping(value = "/ossUpload", method = RequestMethod.POST)
	public JSONObject ossUpload(MultipartFile myimage, String folder) {
		JSONObject jsonObject = getJsonResult(myimage, folder);
		if (jsonObject.getBoolean(SUCCESS)) {
			try {
				// 上传图片到腾讯云
				String key = folder + "/" + Math.abs(UUID.randomUUID().hashCode()) + ".jpg";
				TencentOssUtil tCloud = new TencentOssUtil();
				String url = tCloud.uploadImage(key, myimage.getSize(), myimage.getInputStream());
				jsonObject.put("url", url);
			} catch (Exception e) {
				logger.error("保存失败：" + e.getMessage());
				jsonObject.put(SUCCESS, false);
				jsonObject.put(MESSAGE, "上传失败");
			}
		}
		return jsonObject;
	}
	
	@Override
	protected String getBasePath() {
		return null;
	}
	

}
